package com.wu.flandre.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wu.flandre.entity.common.*;
import com.wu.flandre.service.AdminIndexService;
import com.wu.flandre.util.HttpRequestUtil;
import com.wu.flandre.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.*;
import org.springframework.stereotype.Service;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * AdminIndexServiceImpl <br/>
 *
 * @author xuan.wu <br/>
 * @date 2019/12/29 13:04 <br/>
 */
@Service
@Slf4j
public class AdminIndexServiceImpl implements AdminIndexService {
    @Override
    public ResponseTemp serverInfo() {
        ResponseTemp rest = null;
        try {
            Properties props = System.getProperties();
            OsInformationEntity osInformationEntity = new OsInformationEntity();
            //java 版本
            osInformationEntity.setJavaVersion(props.getProperty("java.version"));
            //java 安装路径
            osInformationEntity.setJavaInstallPath(props.getProperty("java.home"));
            OperatingSystem OS = OperatingSystem.getInstance();
            //系统名字
            osInformationEntity.setOsName(OS.getName());
            //系统架构
            osInformationEntity.setOsArch(OS.getArch());
            //系统描述
            osInformationEntity.setOsDescription(OS.getDescription());
            //系统版本
            osInformationEntity.setOsVersion(OS.getVersion());
            rest = ResponseUtil.success(osInformationEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            rest = ResponseUtil.exceptions("执行异常:" + e.getMessage());
        }
        return rest;
    }

    @Override
    public ResponseTemp serverMemory() {
        ResponseTemp rest = null;
        try {
            Sigar sigar = new Sigar();
            Mem mem = sigar.getMem();
            SystemMemoryEntity systemMemoryEntity = new SystemMemoryEntity();
            // 内存总量
            systemMemoryEntity.setMemoryTotal(mem.getTotal() / 1024L);
            // 当前内存使用量
            systemMemoryEntity.setMemoryUse(mem.getUsed() / 1024L);
            // 当前内存剩余量
            systemMemoryEntity.setMemoryFree(mem.getFree() / 1024L);
            Swap swap = sigar.getSwap();
            // 交换区总量
            systemMemoryEntity.setSwapTotal(swap.getTotal() / 1024L);
            // 当前交换区使用量
            systemMemoryEntity.setSwapUse(swap.getUsed() / 1024L);
            // 当前交换区剩余量
            systemMemoryEntity.setSwapFree(swap.getFree() / 1024L);
            rest = ResponseUtil.success(systemMemoryEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            rest = ResponseUtil.exceptions("执行异常:" + e.getMessage());
        }
        return rest;
    }

    @Override
    public ResponseTemp serverJvm() {
        ResponseTemp rest = null;
        try {
            MemoryMXBean mxb = ManagementFactory.getMemoryMXBean();
            JvmMemoryEntity jvmMemoryEntity = new JvmMemoryEntity();
            //堆内存信息
            MemoryUsage heapMemoryUsage = mxb.getHeapMemoryUsage();
            jvmMemoryEntity.setHeapInit(heapMemoryUsage.getInit() / 1024L);
            jvmMemoryEntity.setHeapMax(heapMemoryUsage.getMax() / 1024L);
            jvmMemoryEntity.setHeapCommit(heapMemoryUsage.getCommitted() / 1024L);
            jvmMemoryEntity.setHeapUsed(heapMemoryUsage.getUsed() / 1024L);
            //非堆内存信息
            MemoryUsage nonHeapMemoryUsage = mxb.getNonHeapMemoryUsage();
            jvmMemoryEntity.setNoHeapInit(nonHeapMemoryUsage.getInit() / 1024L);
            jvmMemoryEntity.setNoHeapMax(nonHeapMemoryUsage.getMax() / 1024L);
            jvmMemoryEntity.setNoHeapCommit(nonHeapMemoryUsage.getCommitted() / 1024L);
            jvmMemoryEntity.setNoHeadUsed(nonHeapMemoryUsage.getUsed() / 1024L);
            rest = ResponseUtil.success(jvmMemoryEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            rest = ResponseUtil.exceptions("执行异常:" + e.getMessage());
        }
        return rest;
    }

    @Override
    public ResponseTemp serverDisk() {
        ResponseTemp rest = null;
        try {
            List<DiskInfoEntity> diskList = new LinkedList<>();
            Sigar sigar = new Sigar();
            FileSystem[] fileSystemList = sigar.getFileSystemList();
            for (FileSystem fs : fileSystemList) {
                if (fs.getType() == 2) {
                    DiskInfoEntity diskInfoEntity = new DiskInfoEntity();
                    diskInfoEntity.setDiskName(fs.getDevName());
                    diskInfoEntity.setDiskTypeName(fs.getSysTypeName());
                    FileSystemUsage fileSystemUsage = sigar.getFileSystemUsage(fs.getDirName());
                    diskInfoEntity.setDiskSizeTotal(fileSystemUsage.getTotal());
                    diskInfoEntity.setDiskSizeFree(fileSystemUsage.getFree());
                    diskInfoEntity.setDiskSizeUsed(fileSystemUsage.getUsed());
                    diskInfoEntity.setDiskPercent(fileSystemUsage.getUsePercent() * 100);
                    diskList.add(diskInfoEntity);
                }
            }
            rest = ResponseUtil.success(diskList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            rest = ResponseUtil.exceptions("执行异常:" + e.getMessage());
        }
        return rest;
    }

    @Override
    public ResponseTemp serverDb() {
        ResponseTemp rest = null;
        try {
            String json = HttpRequestUtil.getInstance().doGet2String("http://localhost:8082/druid/datasource.json");
            ObjectMapper mapper = new ObjectMapper();
            HashMap jsonMap = mapper.readValue(json, HashMap.class);
            ArrayList<HashMap> contentList = (ArrayList) jsonMap.get("Content");
            HashMap currentMap = contentList.get(0);
            Map<String, Object> res = new HashMap<>(8);
            res.put("数据库连接池名字", currentMap.get("Name"));
            res.put("数据库类型", currentMap.get("DbType"));
            res.put("数据库驱动类", currentMap.get("DriverClassName"));
            res.put("数据库URL", currentMap.get("URL"));
            res.put("数据库登录用户", currentMap.get("UserName"));
            res.put("数据库初始化连接数", currentMap.get("InitialSize"));
            res.put("数据库最小连接数", currentMap.get("MinIdle"));
            res.put("数据库最大连接数", currentMap.get("MaxActive"));
            rest = ResponseUtil.success(res);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            rest = ResponseUtil.exceptions("执行异常:" + e.getMessage());
        }
        return rest;
    }
}
